package ycl.security.handler;

import com.alibaba.fastjson.JSONObject;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Service;
import ycl.common.ApiResult;
import ycl.common.utils.ServletUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 未有登录实例
 *
 * @author ycl
 * @date 2022-03-10 13:11:50
 */
@Service("authEntryPoint")
public class AuthEntryPoint implements AuthenticationEntryPoint {
	@Override
	public void commence(
			HttpServletRequest request,
			HttpServletResponse response,
			AuthenticationException e) throws IOException {
		ServletUtil.setResultResponse(response);
		//ApiResult<Object> result = ApiResult.fail(ApiResultEnum2.SC_UNAUTHORIZED);
		ApiResult<Object> result = ApiResult.fail(401, "imya", null);
		response.getWriter().write(JSONObject.toJSONString(result));
		response.getWriter().flush();
	}
}
